#!/bin/bash 

cwd=`dirname $0`
. $cwd/common.sh 


svn_check() {
    #first check svn status
    echo "checking $1 svn status, pls wait!"
    svn_status=`svn st`
    check_status
    if [ ! "$svn_status" = "" ];then
           echo  "Notice svn is dirty!"
           echo "$svn_status"
           loop=true
           while $loop ;do
               read -p "Well build a dirty $1 image, continue? [y/n]" ISCONTINUS
               case $ISCONTINUS in 
                   "y" | "Y") echo "Your choose continue, a dirty imaga well build!";loop=false;break
                        ;;
                    "n" | "N") echo "Your choose stop,exit! try to run \"svn commit\" >>>||$1||<<<"; exit -1 ;;
           esac
           done
    fi
    #sencod make sure svn vsersion newest
    svn up

}

build_kernel() {
    cd $KERNELDIR
    svn_check "kernel"

    cd $BUILDDIR 
    ./build.sh kernel
}

build_uboot() {
    cd $UBOOTDIR
    
    svn_check "uboot"

    cd $BUILDDIR 
    ./build.sh uboot
}

make_updatedir() {
    #1. make sure updatefile created
    rm -rf $OUTPUTDIR/$UPDATEFILE
    mkdir -p $OUTPUTDIR/$UPDATEFILE/img
    cd $OUTPUTDIR/$UPDATEFILE/img

    #2. cp updatescript to updatefile
    cp $UPDATE_SCRIPTDIR/*  $OUTPUTDIR/$UPDATEFILE -a

    #3. cp current version  MLO,uboot,dts,uImage to updatefile
    if [ -d $IMAGEDIR/kernel_$KERNEL_VERSION ];then 
        cp $IMAGEDIR/kernel_$KERNEL_VERSION/* $OUTPUTDIR/$UPDATEFILE/img
        
        rm $IMG_KERNEL $IMG_DTS -rf
        cp -a kernel.$KERNEL_VERSION $IMG_KERNEL
        cp -a dts.$KERNEL_VERSION $IMG_DTS
    else
        echo "Error: can't find $IMAGEDIR/kernel_$KERNEL_VERSION , rm all:$OUTPUTDIR\/$UPDATEFILE"
        rm -rf $OUTPUTDIR/$UPDATEFILE
    fi

    if [ -d $IMAGEDIR/uboot_$KERNEL_VERSION ];then 
        cp $IMAGEDIR/uboot_$KERNEL_VERSION/* $OUTPUTDIR/$UPDATEFILE/img

        rm -f $IMG_UBOOT $IMAG_MLO
        cp -a uboot.$UBOOT_VERSION  $IMG_UBOOT 
        cp -a MLO.$UBOOT_VERSION $IMG_MLO 
    else
        echo "Error: can't find $IMAGEDIR/uboot_$UBOOT_VERSION , rm all:$OUTPUTDIR\/$UPDATEFILE"
        rm -rf $OUTPUTDIR/$UPDATEFILE
    fi
    echo "create success, move to disk!!!"

}

build_uboot

build_kernel

make_updatedir
